package com.yanqu.road.dao.impl.message;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.message.WorldMessage;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WorldMessageDaoImpl extends TempDataObjectDao<WorldMessage> {

    public Map<Integer, WorldMessage> getWorldMessageMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, WorldMessage> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_world_message order by sendTime";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                WorldMessage data = parse(rs);
                dataMap.put(data.getIndex(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(WorldMessage data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_world_message(msgIndex,type,contentType,sendTime,content,userId) values (?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getIndex());
            call.setInt(i++, data.getType());
            call.setInt(i++, data.getContentType());
            call.setLong(i++, data.getSendTime());
            call.setString(i++, data.getContent());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(WorldMessage data) {
        PreparedStatement call = null;
        String sql = "update t_u_world_message set type=?,contentType=?,sendTime=?,content=?,userId=? where msgIndex=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getType());
            call.setInt(i++, data.getContentType());
            call.setLong(i++, data.getSendTime());
            call.setString(i++, data.getContent());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getIndex());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected WorldMessage parseData(ResultSet rs) throws SQLException {
        WorldMessage data = new WorldMessage();
        data.setIndex(rs.getInt("msgIndex"));
        data.setType(rs.getInt("type"));
        data.setContentType(rs.getInt("contentType"));
        data.setSendTime(rs.getLong("sendTime"));
        data.setContent(rs.getString("content"));
        data.setUserId(rs.getLong("userId"));
        return data;
    }
}
